package com.yb.login.center.controller;

import cn.hutool.core.util.DesensitizedUtil;
import com.yb.core.annotation.Decrypt;
import com.yb.core.annotation.Desensitize;
import com.yb.core.annotation.RateLimit;
import com.yb.core.bean.Resp;
import com.yb.core.constant.HttpConstant;
import com.yb.login.center.service.IUserService;
import com.yb.login.center.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/12 22:11
 * FileName: LoginController
 * Description:
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    @RequestMapping("/login")
    @Decrypt(fields = {"mobile"})
    public Resp login(@RequestBody LoginVo loginVo){

        return userService.login(loginVo);
    }

    @RequestMapping("/logout")
    public Resp logout(@RequestHeader(name= HttpConstant.AUTH_HEADER) String authentication){
        return userService.logout(authentication);
    }


    @RateLimit(key = "#loginVo.mobile",count = 1,time = 60,ip = false)
    @PostMapping("/login/verifyCode")
    @Decrypt(fields = {"mobile"})
    //@Validated(GetVerityCode.class)
    public Resp verifyCode(@RequestBody  LoginVo loginVo, HttpServletResponse response){
        return userService.verifyCode(loginVo,response);
    }


    @RateLimit(ip = true,count = 4,time = 1)//限流通过ip，一秒内只能访问一次
    @GetMapping("/login/imagecode")
    public Resp verifyImgCode(HttpServletRequest request,HttpServletResponse response){
        return userService.verifyImgCode(request,response);
    }


    @Desensitize(fields = {"mobile","phone"},type = DesensitizedUtil.DesensitizedType.MOBILE_PHONE)
    @Desensitize(fields = {"idcard"},type = DesensitizedUtil.DesensitizedType.ID_CARD)

    @PostMapping("/test")
    public Resp test(@RequestBody  LoginVo loginVo){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("mobile","18501349133");
        map.put("phone","18501349133");

        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("idcard","613413333211233456");
        ArrayList<Object> list = new ArrayList<>();
        list.add(map);
        list.add(map1);
        return Resp.ok().data(list);
    }


    @Decrypt(fields = {"mobile","phone"})
    @PostMapping("/decrypt")
    public Resp decrypt(@RequestBody  LoginVo loginVo){
        return Resp.ok().data(loginVo);
    }
}



